package com.my.control
{
	import com.my.control.validate.RegisterViewValidate;
	import com.my.model.bean.UserInfoEntity;
	import com.my.service.RemoteServiceFactory;
	import com.my.view.popup.RegisterView;
	
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.core.UIComponent;
	import mx.rpc.events.ResultEvent;

	public class RegisterViewControl extends AbstractControl implements IControl
	{
		private static var instance : RegisterViewControl = null;
		
		private var registerValidate : RegisterViewValidate;
		
		public function RegisterViewControl(value:UIComponent)
		{
			super(value);
			registerValidate = new RegisterViewValidate(value);
		}
		
		public static function getInstance():RegisterViewControl{
			return instance;
		} 
		
		public static function startup(value:UIComponent):void{
			instance = new RegisterViewControl(value);
		}
		
		public override function addEventListener():void{
			getView().registerBt.addEventListener(MouseEvent.CLICK,registerBtCilckHandler);
		}
		
		public function registerBtCilckHandler(event:MouseEvent):void{
			if(registerValidate.validate()){
				var userInfo : UserInfoEntity = new UserInfoEntity();
				userInfo.username = getView().username.text;
				userInfo.password = getView().password.text;
				userInfo.email = getView().email.text;
				userInfo.question = getView().isLockCombo.value.toString();
				userInfo.reply = getView().question.text;
				
				RemoteServiceFactory.getInstance().userInfoService.addEventListener(ResultEvent.RESULT,function(e:ResultEvent):void{
					RemoteServiceFactory.getInstance().userInfoService.removeEventListener(ResultEvent.RESULT,arguments.callee);
				
					var v : int = e.result as int;
					if(v == 1){
						Alert.show("注册成功");
						getView().closeWin();
					}else if(v == -1){
						getView().registerResult.text = "用户名已存在";
						getView().registerCodePic.loadCode();
					}else if(v == -2){
						getView().registerResult.text = "验证码错误";
						getView().registerCodePic.loadCode();
					}else{
						Alert.show("");
						getView().registerCodePic.loadCode();
					}
					
				});
				RemoteServiceFactory.getInstance().userInfoService.addUserInfo(userInfo,getView().check.text);
			}
		}
		
		
		
		public function getView():RegisterView{
			return RegisterView(view);
		} 
	}
}